package com.dhcc.stream;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: Stream_01
 * @Description: 初玩Stream
 * @author weiyongpeng
 * @date 2023年5月7日 下午3:11:47
 */
public class Stream_01 {
	public static void main(String[] args) {
		// 声明集合
		ArrayList<String> list = new ArrayList<>();
		list.add("张三");
		list.add("李四");
		list.add("王五");
		list.add("孙琪");
		list.add("悟空");
		list.add("八戒");
		list.add("张无忌");
		// 把所有的张姓添加到新集合里
		ArrayList<String> zhangFirstNameList = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).startsWith("张")) {
				zhangFirstNameList.add(list.get(i));
			}
		}
		
		zhangFirstNameList.forEach(System.out::println);
		System.out.println("--------------------------------");
		// 把所有张姓开头的，长度为三的放到新集合里
		ArrayList<String> zhangFirstNameLength3 = new ArrayList<>();
		for (int i = 0; i < zhangFirstNameList.size(); i++) {
			if(zhangFirstNameList.get(i).length() == 3) {
				zhangFirstNameLength3.add(zhangFirstNameList.get(i));
			}
		}
		zhangFirstNameLength3.forEach(System.out::println);
		System.out.println("--------------------------------");
		
		// 上述的写法很麻烦
		System.out.println("Stream流的写法");
		list.stream().filter(name->name.startsWith("张")).filter(name->name.length()==3).forEach(System.out::println);
	}
}
